import axios from './index'
import { Banner, Types, Video, User, Cart, Collect, Address, Notice } from './api'
import { getCookie } from '@/utils/cookie'

//获取banner的请求
export function banner() {
    return axios(Banner)
}

//获取所有分类
export function getTypes() {
    return axios(Types).then(data => {
        data = data.map(item => {
            delete item.products
            return item
        })
        return new Promise(resolve => resolve(data))
    })
}

//获取分类商品
export function getTypesAndPros() {
    return axios(Types)
}

//获取所有商品
export function getPros() {
    return axios(Types).then(data => {
        let pros = []
        data.forEach(type => pros.push(...type.products))
        return new Promise(resolve => resolve(pros))
    })
}

//获取所有视频
export function getVideo() {
    return axios(Video)
}

//注册
export function register(data) {
    return axios(User, {
        method: 'post',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: data
    })
}

//登录
export function login(data) {
    return axios(User).then(users => {
        let res = users.find(user => user.username === data.username && user.password === data.password)
        console.log(res);
        let resData = {}
        if (res) {
            resData.error_code = 200
            resData.msg = '登录成功！'
        } else {
            resData.error_code = 400
            resData.msg = '用户名或密码错误！'
        }
        return new Promise(resolve => resolve(resData))
    })
}

//获取商品详情
export function getProDetail(typeid, proid) {
    return axios(Types).then(types => {
        let type = types.find(item => item.id === typeid);
        let pro = type.products.find(item => item.id === proid);
        return new Promise(resolve => resolve(pro))
    })
}

//获取购物车
export function getCarts() {
    return axios(Cart).then(carts => {

        return new Promise(resolve => resolve(carts))
    })
}

//关键字搜索
export function searchPros(keywords) {
    return getPros().then(data => {
        let searchPros = data.filter(item => item.name.includes(keywords));
        return new Promise(resolve => resolve(searchPros))
    })
}

//获取某个分类下所有商品
export function getTypePros(typeid) {
    return axios(Types + '/' + typeid).then(data => {
        return new Promise(resolve => resolve(data.products))
    })
}

//加入购物车
export function addcarts(data) {
    return getCarts().then(item => {
        let currentData = item.find(v => v.proid === data.proid)
        if (currentData) {
            currentData.number++
            return axios(Cart + '/' + currentData.id, {
                method: 'put',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: currentData
            })
        }
        //不存在，新增
        return axios(Cart, {
            method: 'post',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            data
        })
    })
}

//获取用户的收货地址
export function getAddress() {
    return axios(Address).then(data => {
        let username = getCookie('username')
        let userAddress = data.filter(item => item.username === username)
        userAddress = userAddress.map(item => {
            if (item.isDafault === 'true') {
                item.isDafault = true
            } else {
                item.isDafault = false
            }
            return item
        })
        return new Promise(resolve => resolve(userAddress))
    })
}

//保存地址
export function addAddress(data) {
    return axios(Address, {
        method: 'post',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data
    })
}

// 默认地址
export function getDefaultAddress() {
    return axios(Address).then(data => {
        let username = getCookie('username')
        let userAddress = data.filter(item => item.username === username)
        let isDafault = userAddress.find(item => item.isDafault)
        console.log(isDafault);
        return new Promise(resolve => resolve(isDafault.address))
    })
}

//修改地址
export function editAddress(data) {
    console.log(data);
    return axios(Address + '/' + data.id, {
        method: 'put',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data
    })
}

// 获取用户信息
export function getUser(username){
    return axios.get(User).then(data=>{
        var arr = data.filter(item => item.username === username)
        return new Promise(resolve=>resolve(arr))
        }
    )
}

//修改用户信息
export function putUser(user){
    return axios.put('/User/'+user.id,user).then(data=>{
        return new Promise(resolve => resolve({code:0,msg:'修改成功'}))
    })
}